//
//  NSDate+HHExtension.m
//  iOSDevelopSDK
//
//  Created by Hayder on 2019/6/13.
//  Copyright © 2019 Hayder. All rights reserved.
//

#import "NSDate+HHExtension.h"

@implementation NSDate (HHExtension)

/**1.date根据时间格式转成string*/
- (NSString *)dateToStringWithFormat:(NSString *)format;
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    //    [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];//解决8小时时间差问题
    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
    return [dateFormatter stringFromDate:self];
}

/**2.字符串根据时间格式转成date*/
+ (NSDate *)stringToDate:(NSString *)timeString dateFormat:(NSString *)format
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    [dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
    return [dateFormatter dateFromString:timeString];
}

/** 3.获取当前的时间 yyyy-MM-dd HH:mm:ss*/
+ (NSString *)currentDateString {
    
    return [self currentDateStringWithFormat:@"yyyy-MM-dd HH:mm:ss"];
}

/** 4.按指定格式获取当前的时间*/
+ (NSString *)currentDateStringWithFormat:(NSString *)format {
    // 获取系统当前时间
    NSDate *currentDate = [NSDate date];
    // 用于格式化NSDate对象
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    // 设置格式：yyyy-MM-dd HH:mm:ss
    formatter.dateFormat = format;
    // 将 NSDate 按 formatter格式 转成 NSString
    NSString *currentDateStr = [formatter stringFromDate:currentDate];
    // 输出currentDateStr
    return currentDateStr;
}

/**5.获取时间串前几秒的时间*/
+ (NSString *)calculateTimeAnySecondsAgo:(double)second WithConvertTimeStr:(NSString *)timeStr
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *currentDate = [formatter dateFromString:timeStr];
    NSTimeInterval currentT = [currentDate timeIntervalSince1970];
    NSTimeInterval resultT = currentT - second;
    NSDate *resultDate = [NSDate dateWithTimeIntervalSince1970:resultT];
    return [formatter stringFromDate:resultDate];
}

/**6.获取时间串后几秒的时间*/
+ (NSString *)calculateTimeAnySecondsAfter:(double)second WithConvertTimeStr:(NSString *)timeStr
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *currentDate = [formatter dateFromString:timeStr];
    NSTimeInterval currentT = [currentDate timeIntervalSince1970];
    NSTimeInterval resultT = currentT + second;
    NSDate *resultDate = [NSDate dateWithTimeIntervalSince1970:resultT];
    return [formatter stringFromDate:resultDate];
}

/**7.获取时长:自己判断时间段  刚刚，1分钟前..*/
+ (NSString *)calSubscribeTimeWithTime:(NSString *)time
{
    //把字符串转为NSdate
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *timeDate = [dateFormatter dateFromString:time];
    //得到与当前时间差 s
    NSTimeInterval timeInterval = [timeDate timeIntervalSinceNow];
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }else if((temp = timeInterval/60) < 60){
        result = [NSString stringWithFormat:@"%ld%@",temp, @"分钟"];
    }else
    {
        NSArray *dateAndTime = [time componentsSeparatedByString:@" "];
        if (dateAndTime.count > 0) {
            result = [NSString stringWithFormat:@"%@ %@",dateAndTime[0], @"开始"];
        }else
        {
            result = @"";
        }
        
    }
    return  result;
}

+ (BOOL)startTime:(NSString *)startTime isEarlyThanEndTime:(NSString *)endTime Format:(NSString *)format
{
    NSString *dateFormat = format?format:@"yyyy-MM-dd HH:mm:ss";
    //按照日期格式创建日期格式句柄
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:dateFormat];
    NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
    [dateFormatter setTimeZone:localTimeZone];
    //将日期字符串转换成Date类型
    NSDate *startDate = [dateFormatter dateFromString:startTime];
    NSDate *endDate = [dateFormatter dateFromString:endTime];
    //将日期转换成时间戳
    NSTimeInterval start = [startDate timeIntervalSince1970]*1;
    NSTimeInterval end = [endDate timeIntervalSince1970]*1;
    NSTimeInterval value = end - start;
    
    if (value > 0) {
        return YES;
    }else
    {
        return NO;
    }
}

/**9.当前时间与另一个时间的时间差*/
+ (NSInteger)currentDateDeffientWithOtherDate:(NSString *)otherDateString dateFormat:(NSString *)format
{
    NSString *currentDateString = [self currentDateStringWithFormat:format];
    //    NSString *currentDateString = @"2022-01-01";
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    NSTimeZone *localTimeZone = [NSTimeZone localTimeZone];
    [dateFormatter setTimeZone:localTimeZone];
    
    NSDate *currentDate = [dateFormatter dateFromString:currentDateString];
    NSDate *otherDate = [dateFormatter dateFromString:otherDateString];
    
    //利用NSCalendar比较日期的差异
    NSCalendar *calendar = [NSCalendar currentCalendar];
    /**
     * 要比较的时间单位,常用如下,可以同时传：
     *    NSCalendarUnitDay : 天
     *    NSCalendarUnitYear : 年
     *    NSCalendarUnitMonth : 月
     *    NSCalendarUnitHour : 时
     *    NSCalendarUnitMinute : 分
     *    NSCalendarUnitSecond : 秒
     */
    NSCalendarUnit unit = NSCalendarUnitDay;//只比较天数差异
    //比较的结果是NSDateComponents类对象
    NSDateComponents *delta = [calendar components:unit fromDate:otherDate toDate:currentDate options:0];
    //打印
    NSLog(@"%@",delta);
    //获取其中的"天"
    NSLog(@"%ld",delta.day);
    
    return delta.day;
}

//获取N天后日期
+ (NSDate *)getDate:(NSDate *)currentDate day:(NSInteger)day {
    
    NSInteger days = day;    // n天后的天数
    //    days = (days == 0 ? 2.f : days);//未指定天数则默认为两天
    NSDate *appointDate;    // 指定日期声明
    NSTimeInterval oneDay = 24 * 60 * 60;  // 一天一共有多少秒
    appointDate = [currentDate initWithTimeIntervalSinceNow: +(oneDay * days)];
    return appointDate;
}

+(BOOL)date:(NSDate*)date isBetweenDate:(NSDate*)beginDate endDate:(NSDate*)endDate
{
    if ([date compare:beginDate] == NSOrderedDescending && [date compare:endDate] == NSOrderedAscending) {
        return YES;
    }
    return NO;
}

/**
 将一个格式的时间转成另一个格式的时间
 */
+ (NSString *)covertTimeString:(NSString *)timeString stringFormat:(NSString *)stringFormat targetFormat:(NSString *)targetFormat
{
    //    + (NSDate *)stringToDate:(NSString *)timeString dateFormat:(NSString *)format
    NSDate *date = [self stringToDate:timeString dateFormat:stringFormat];
    // 用于格式化NSDate对象
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = targetFormat;
    NSString *targetString = [formatter stringFromDate:date];
    return targetString;
}

+ (NSString*)getWeekDay:(NSString*)currentStr
{
    NSDateFormatter* dateFormat = [[NSDateFormatter alloc]init];//实例化一个NSDateFormatter对象
    [dateFormat setDateFormat:@"yyyy-MM-dd"];//设定时间格式,要注意跟下面的dateString匹配，否则日起将无效
    NSDate*date =[dateFormat dateFromString:currentStr];
    NSArray*weekdays = [NSArray arrayWithObjects: [NSNull null],@"周日",@"周一",@"周二",@"周三",@"周四",@"周五",@"周六",nil];
    NSCalendar*calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSTimeZone*timeZone = [[NSTimeZone alloc]initWithName:@"Asia/Shanghai"];
    [calendar setTimeZone: timeZone];
    NSCalendarUnit calendarUnit =NSCalendarUnitWeekday;
    NSDateComponents*theComponents = [calendar components:calendarUnit fromDate:date];
    return[weekdays objectAtIndex:theComponents.weekday];
}

+ (NSString*)getWeekDayWtithNameXinQi
{
    NSDate *date = [self stringToDate:[self currentDateString] dateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSArray*weekdays = [NSArray arrayWithObjects: [NSNull null],@"星期天",@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六",nil];
    NSCalendar*calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSTimeZone*timeZone = [[NSTimeZone alloc]initWithName:@"Asia/Shanghai"];
    [calendar setTimeZone: timeZone];
    NSCalendarUnit calendarUnit =NSCalendarUnitWeekday;
    NSDateComponents*theComponents = [calendar components:calendarUnit fromDate:date];
    return[weekdays objectAtIndex:theComponents.weekday];
}

+(NSString*)getChineseCalendarMothAndDay{
    
    NSDate *date = [self stringToDate:[self currentDateString] dateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSArray *chineseMonths=@[@"正月", @"二月", @"三月", @"四月", @"五月", @"六月", @"七月", @"八月",
                             @"九月", @"十月", @"冬月", @"腊月"];
    
    NSArray *chineseDays=@[@"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十",
                           @"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十",
                           @"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十"];
    
    NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
    unsigned unitFlags = NSCalendarUnitMonth |  NSCalendarUnitDay;
    NSDateComponents *localeComp = [localeCalendar components:unitFlags fromDate:date];
    
    NSString *m_str = [chineseMonths objectAtIndex:localeComp.month-1];
    NSString *d_str = [chineseDays objectAtIndex:localeComp.day-1];
    
    NSString *chineseCal_str =[NSString stringWithFormat: @"%@%@",m_str,d_str];
    
    return chineseCal_str;
}

+ (NSString *)getChineseCalendarYear
{
    NSDate *date = [self stringToDate:[self currentDateString] dateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSArray *chineseYears = [NSArray arrayWithObjects:
                             @"甲子", @"乙丑", @"丙寅", @"丁卯",  @"戊辰",  @"己巳",  @"庚午",  @"辛未",  @"壬申",  @"癸酉",
                             @"甲戌",   @"乙亥",  @"丙子",  @"丁丑", @"戊寅",   @"己卯",  @"庚辰",  @"辛己",  @"壬午",  @"癸未",
                             @"甲申",   @"乙酉",  @"丙戌",  @"丁亥",  @"戊子",  @"己丑",  @"庚寅",  @"辛卯",  @"壬辰",  @"癸巳",
                             @"甲午",   @"乙未",  @"丙申",  @"丁酉",  @"戊戌",  @"己亥",  @"庚子",  @"辛丑",  @"壬寅",  @"癸丑",
                             @"甲辰",   @"乙巳",  @"丙午",  @"丁未",  @"戊申",  @"己酉",  @"庚戌",  @"辛亥",  @"壬子",  @"癸丑",
                             @"甲寅",   @"乙卯",  @"丙辰",  @"丁巳",  @"戊午",  @"己未",  @"庚申",  @"辛酉",  @"壬戌",  @"癸亥", nil];
    NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
    unsigned unitFlags = NSCalendarUnitYear;
    NSDateComponents *localeComp = [localeCalendar components:unitFlags fromDate:date];
    
    NSString *y_str = [chineseYears objectAtIndex:localeComp.year-1];
    
    return y_str;
}


+(NSString *)getZodiacWithYear
{
    NSDate *date = [self stringToDate:[self currentDateString] dateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *yearString = [date dateToStringWithFormat:@"yyyy"];
    NSInteger y = [yearString integerValue];
    if (y <0) {
        return @"错误日期格式!!!";
    }
    NSString *zodiacString = @"鼠牛虎兔龙蛇马羊猴鸡狗猪";
    NSRange range = NSMakeRange ((y+9)%12-1, 1);
    NSString*  result = [zodiacString substringWithRange:range];
    return [result stringByAppendingString:@"年"];
    
}

@end
